<?php


namespace App\Http\Controllers\Api\V1\Front\Area;


use App\Http\Controllers\Controller;
use App\Models\Province;
use App\Models\City;
use App\Models\Area;
use App\Libs\httpUtil;
use App\Services\Front\AreaPinYinService;
use Illuminate\Http\Request;

class AreaController extends Controller
{
    public function index(Request $request)
    {   
        //准备创建首字母
        // $cityList = City::select('id','city','key')->get();
        // $area = new AreaService();
        // foreach ($cityList as $key => $value) {
        //     $up[$key]['id'] = $value['id'];
        //     $up[$key]['key'] = $area->str($value['city']);
        //     $cityId[$key] = $value['id'];
        // }
        // //准备批量更新语句
        // $sql = 'update city set `key` = case ';
        // $up = array_values($up);
        // foreach ($up as $key => $value) {
        //     $sql = $sql." when id = ".$value['id'].' then "'.$value['key'].'"';
        // }
        // $sql = $sql.' end where id in ('.implode(',',$cityId).')';
        
        // //批量更新
        // \DB::update(\DB::raw($sql));
        
        
        // return var_dump($up);


        $hostCityList = City::select('city')->where('is_host',1)->get();
        $city = City::select('city','key')->get()->toArray();

        $cityList=[];
        foreach ($city as $key => $value) {
            $cityList[$value['key']][] = $value;
        }
        reset($cityList);
        $data['hostCityList'] = $hostCityList;
        $data['cityList'] = $cityList;
        return $this->apiResponse($data);
    }

    public function searchArea(Request $request)
    {   
//=============================================================================================
        // //省的拼音提取
        // $province = Province::select('id','province')->get()->toArray();
        // $provincePinyin = [];
        // $pinyin = new AreaPinYinService();
        // foreach ($province as $key => $value) {
        //     $provinceId[] = $value['id'];
        //     $provincePinyin[$key]['id'] = $value['id'];
        //     $str=iconv("UTF-8","gbk//TRANSLIT",$value['province']);
        //     $str = $pinyin->getFullSpell($str);
        //     $provincePinyin[$key]['spell'] = $str;
        // }
        // //准备批量更新语句
        // $sql = 'update province set `spell` = case ';
        // $provincePinyin = array_values($provincePinyin);
        // foreach ($provincePinyin as $key => $value) {
        //     $sql = $sql." when id = ".$value['id'].' then "'.$value['spell'].'"';
        // }
        // $sql = $sql.' end where id in ('.implode(',',$provinceId).')';
        
        // //批量更新
        // \DB::update(\DB::raw($sql));
        // return var_dump($provincePinyin);
//=============================================================================================
        // //市的拼音提取
        // $city = City::select('id','city')->get()->toArray();
        // $cityPinyin = [];
        // $pinyin = new AreaPinYinService();
        // foreach ($city as $key => $value) {
        //     $cityId[] = $value['id'];
        //     $cityPinyin[$key]['id'] = $value['id'];
        //     $str=iconv("UTF-8","gbk//TRANSLIT",$value['city']);
        //     $str = $pinyin->getFullSpell($str);
        //     $cityPinyin[$key]['spell'] = $str;
        // }
        // //准备批量更新语句
        // $sql = 'update city set `spell` = case ';
        // $cityPinyin = array_values($cityPinyin);
        // foreach ($cityPinyin as $key => $value) {
        //     $sql = $sql." when id = ".$value['id'].' then "'.$value['spell'].'"';
        // }
        // $sql = $sql.' end where id in ('.implode(',',$cityId).')';
        
        // //批量更新
        // \DB::update(\DB::raw($sql));
        // return var_dump($cityPinyin);
//=============================================================================================        
        // //县区的拼音提取
        // $area = Area::select('id','area')->get()->toArray();
        // $areaPinyin = [];
        // $pinyin = new AreaPinYinService();
        // foreach ($area as $key => $value) {
        //     $areaId[] = $value['id'];
        //     $areaPinyin[$key]['id'] = $value['id'];
        //     $str=iconv("UTF-8","gbk//TRANSLIT",$value['area']);
        //     $str = $pinyin->getFullSpell($str);
        //     $areaPinyin[$key]['spell'] = $str;
        // }
        // //准备批量更新语句
        // $sql = 'update area set `spell` = case ';
        // $areaPinyin = array_values($areaPinyin);
        // foreach ($areaPinyin as $key => $value) {
        //     $sql = $sql." when id = ".$value['id'].' then "'.$value['spell'].'"';
        // }
        // $sql = $sql.' end where id in ('.implode(',',$areaId).')';
        
        // //批量更新
        // \DB::update(\DB::raw($sql));
        // return var_dump($areaPinyin);
//=============================================================================================
        $keyWords = $request->keyWords;
        if(!$keyWords){
            return $this->apiResponse(null);
        }
        // $province = Province::select('province')
        //                 ->where(function($query) use ($keyWords){
        //                     $query->where('province','like','%'.$keyWords.'%')->orWhere(function($query) use ($keyWords){
        //                         $query->where('spell','like','%'.$keyWords.'%');
        //                     });
        //                 })->get()->toArray();
        $city = City::select('city')
                    ->where(function($query) use ($keyWords){
                        $query->where('City','like','%'.$keyWords.'%')->orWhere(function($query) use ($keyWords){
                            $query->where('spell','like','%'.$keyWords.'%');
                        });
                    })->get()->toArray();  
        // $area = Area::select('area')
        //             ->where(function($query) use ($keyWords){
        //                 $query->where('area','like','%'.$keyWords.'%')->orWhere(function($query) use ($keyWords){
        //                     $query->where('spell','like','%'.$keyWords.'%');
        //                 });
        //             })->get()->toArray();  
        // $postList = array_values(array_merge($area,array_merge($province,$city)));
        return $this->apiResponse($city);
    }

    /**
     * 经纬度转换
     * @param Request $request [description]
     */
    public function AddressComponent(Request $request)
    {
        $longitude = $request->longitude;
        $latitude = $request->latitude;
        if(!$longitude || !$latitude){
            return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
        }
        $location = $latitude.','.$longitude;
        $key = "64JBZ-2YOE3-WRW3C-YC77P-QO3XO-5MFXM";
        $url = "https://apis.map.qq.com/ws/geocoder/v1/?location=".$location."&key=".$key."&output=json";
        //return var_dump($url);
        $http = new httpUtil();
        $result = $http->http_get($url);
        $result = json_decode($result);
        $result = $result->result;
        $address_component = $result->address_component;
        $data['province'] = $address_component->province;
        $data['city'] = $address_component->city;
        $data['area'] = $address_component->district;
        return $this->apiResponse($data);
    }
}